//
//  addContactsViewController.m
//  Findif
//
//  Created by Mac on 2019/2/18.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "addContactsViewController.h"
#import "AddContactsHeaderView.h"
#import "AddGroupHeaderView.h"
#import "AddGroupTableViewCell.h"
#import "ApplyEnterGroupViewController.h"
#import "SetUpGroupViewController.h"
#import "GroupInfoViewController.h"
#import "SearchContactsViewController.h"
#import "GroupModel.h"
#import "SearchPersonViewController.h"
#import "SearchPersonModel.h"
#import "SearchGroupViewController.h"
#import "AddPersonHeaderView.h"
#import "BaseAlertController.h"
#import "MyFansTableViewCell.h"
#import "PersonalViewController.h"
#import <CoreLocation/CLLocationManager.h>
#import <CoreLocation/CoreLocation.h>

@interface addContactsViewController ()<UITableViewDataSource,UITableViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource,AddContactsHeaderViewDelegate,AddGroupHeaderViewDelegate,AddPersonHeaderViewDelegate,CLLocationManagerDelegate>{
    
    NSInteger _ageIndex;
}
@property (nonatomic, strong)AddContactsHeaderView * addView;
@property (nonatomic, strong)AddPersonHeaderView * header;
@property (nonatomic, strong)NSArray<GroupModel *> * modelArray;
@property (nonatomic, weak)UITableView * personTableView;
//@property (nonatomic, strong)NSArray * titleArray;
@property (nonatomic, copy)NSString * status;
@property (nonatomic, weak)UIButton * rightButton;
//@property (nonatomic, weak)UIButton * searchButton;

@property (nonatomic, strong)BaseAlertController * genderAlertVC;
@property (nonatomic, copy)NSString * age;
@property (nonatomic, copy)NSString * ageid;
@property (nonatomic, strong)NSMutableArray * dataArray;
@property (nonatomic, strong)UIView * bgView;
@property (nonatomic, strong)UIPickerView * pickerView;

@property (nonatomic, strong)NSIndexPath * indexPath;
@property (nonatomic, copy)NSString * sex;
@property (nonatomic, copy)NSString * searchStr;

@property (nonatomic, strong)NSArray <SearchPersonModel *> * searchArray;
@property (nonatomic, strong)NSArray <SearchPersonModel *> * personArray;
@property (nonatomic, strong)NSArray <GroupModel *> * searchGroupArray;
@property (nonatomic, weak)UILabel * showLabel;

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation addContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.titleArray = @[@"按条件查找陌生人",@"性别",@"年龄"];
    self.status = @"person";
    self.dataArray = [NSMutableArray array];
    _ageIndex = 0;
    [self requestNearbyPeople];
    [self loadData];
    [self setupNav];
    [self setupUI];
    [self setupGenderAlertVC];
    [self setupPivkerView];
}


- (void)setPersonArray:(NSArray<SearchPersonModel *> *)personArray{
    
    _personArray = personArray;
    
    [self.personTableView reloadData];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getAgeListResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.dataArray = [NSMutableArray arrayWithArray:responseData[@"data"]];
            NSDictionary * dict = @{@"age":@"不限",@"id":@""};
            [self.dataArray addObject:dict];
            
            [self.pickerView reloadAllComponents];
        }
    }];

    
    [[ZRNetworkTool sharedTool] getRandGroupListWithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.modelArray = [NSArray yy_modelArrayWithClass:[GroupModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.personTableView reloadData];
}


- (void)setStatus:(NSString *)status{
    
    _status = status;
    
    if ([status isEqualToString:@"person"]) {
        
        self.rightButton.hidden = YES;
//        self.searchButton.hidden = NO;
    }else{
        
        self.rightButton.hidden = NO;
//        self.searchButton.hidden = YES;
    }
    
    [self.personTableView reloadData];
}


- (void)setupNav{
    
    self.title = @"添加";
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"创建" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didSetUpButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    rightButton.hidden = YES;
    
    self.rightButton = rightButton;
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    self.addView = [[AddContactsHeaderView alloc] initWithFrame:CGRectMake(0, topHeight, kScreenW, 95 * SCREENH6)];
    self.addView.delegate = self;
    [self.view addSubview:self.addView];
    
    UITableView * personTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight + 95 * SCREENH6, kScreenW, kScreenH - topHeight - 95 * SCREENH6) style:UITableViewStylePlain];
    personTableView.delegate = self;
    personTableView.dataSource = self;
    personTableView.backgroundColor = ColorWithRGB(0xF9F9F9);
//    personTableView.rowHeight = 50 * SCREENH6;
    personTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    personTableView.showsVerticalScrollIndicator = NO;
//    personTableView.userInteractionEnabled = YES;
//    //创建轻拍手势
//    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
//    [personTableView addSubview:tapGR];
    
    self.personTableView = personTableView;
    
    [self.personTableView registerClass:[MyFansTableViewCell class] forCellReuseIdentifier:@"personTableViewCell"];
    [self.personTableView registerClass:[AddGroupTableViewCell class] forCellReuseIdentifier:@"AddGroupTableViewCell"];
    [self.personTableView registerClass:[AddGroupHeaderView class] forHeaderFooterViewReuseIdentifier:@"AddGroupHeaderView"];
    [self.personTableView registerClass:[AddPersonHeaderView class] forHeaderFooterViewReuseIdentifier:@"AddPersonHeaderView"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self loadData];
//    }];
    
    [self.view addSubview:self.personTableView];
    
    
//    UIButton * searchButton = [[UIButton alloc] initWithFrame:CGRectMake(60 * SCREENH6, topHeight + 330 * SCREENH6, 256 * SCREENH6, 30 * SCREENH6)];
//
//    [searchButton setTitle:@"立即查找" forState:UIControlStateNormal];
//    [searchButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
//    [searchButton setBackgroundColor:ColorWithRGB(0xFFE035)];
//    [searchButton addTarget:self action:@selector(clickSearchButton) forControlEvents:UIControlEventTouchUpInside];
//    searchButton.titleLabel.font = [UIFont systemFontOfSize:16 * SCREENH6];
//    searchButton.layer.cornerRadius = 5 * SCREENH6;
//    searchButton.layer.masksToBounds = YES;
//
//    self.searchButton = searchButton;
//
//    [self.personTableView addSubview:self.searchButton];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"开启定位后，才能准确的为您推荐附近的人~";
    showLabel.textColor = ZRBlackColor;
    showLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    showLabel.textAlignment = NSTextAlignmentCenter;
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.personTableView addSubview:self.showLabel];
}


-(void)viewDidLayoutSubviews{
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.personTableView);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.status isEqualToString:@"person"]) {
        
        return self.personArray.count;
    }else{
        
        return self.modelArray.count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.status isEqualToString:@"person"]) {
        
        MyFansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"personTableViewCell" forIndexPath:indexPath];

        cell.searchModel = self.personArray[indexPath.row];
        
        return cell;
    }else{
        
        AddGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddGroupTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.modelArray[indexPath.row];
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.status isEqualToString:@"person"]) {
        
        return 60 * SCREENH6;
    }else{
        
        return 70 * SCREENH6;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if ([self.status isEqualToString:@"group"]) {
        
        return 110 * SCREENH6;
    }else{
        
        return 40 * SCREENH6;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if ([self.status isEqualToString:@"group"]) {
        
        AddGroupHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"AddGroupHeaderView"];
        header.delegate = self;
        header.userInteractionEnabled = YES;
        return header;
    }else{
        
        self.header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"AddPersonHeaderView"];
        self.header.delegate = self;
        self.header.userInteractionEnabled = YES;
        
        return self.header;
    }
    
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{

    if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
        ((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = ColorWithRGB(0xF4F5F6);
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.status isEqualToString:@"group"]) {
        
        HiddenBar;
        GroupModel * model = self.modelArray[indexPath.row];
        GroupInfoViewController * applyVC = [GroupInfoViewController new];
        NSString * ID = [NSString stringWithFormat:@"%@",model.group_jg_gid];
        applyVC.group_jg_gid = ID;
        
        [self.navigationController pushViewController:applyVC animated:YES];
        
    }else{
        
        SearchPersonModel * model = self.personArray[indexPath.row];
        
        NSString * ID = [NSString stringWithFormat:@"%@",model.id];
        
        HiddenBar;
        PersonalViewController * personVC = [PersonalViewController new];
        
        personVC.ID = ID;
        
        [self.navigationController pushViewController:personVC animated:YES];
    }
}


- (void)searchTypeWithType:(NSString *)type{
    
    if ([type isEqualToString:@"age"]) {
        
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(0, kScreenH - kScreenH / 3, kScreenW, kScreenH / 3);
        }];
        
    }else{
      
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            
            self.genderAlertVC.popoverPresentationController.sourceView = self.view;
            self.genderAlertVC.popoverPresentationController.sourceRect = CGRectMake(10 * SCREENH6,kScreenH - 150 * SCREENH6,355 * SCREENH6,150 * SCREENH6);
            //            self.alertVC.modalPresentationStyle = UIModalPresentationFormSheet;
        }
        [self presentViewController:self.genderAlertVC animated:YES completion:nil];
    }
}


- (void)didSetUpButton{
    
//    NSString * user_id = DEF_GET_OBJECT(@"id");
//    [[ZRNetworkTool sharedTool] judgeUserIsCreateGroupWithUser_idt:user_id WithBlock:^(BOOL success, id responseData) {
//
//        if (success) {
    
            HiddenBar;
            
            SetUpGroupViewController * setupVC = [SetUpGroupViewController new];
            
            [self.navigationController pushViewController:setupVC animated:YES];

//        }else{
//
//            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                           message:responseData[@"message"]
//                                                                    preferredStyle:UIAlertControllerStyleAlert];
//
//            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                                  handler:^(UIAlertAction * action) {
//
//                                                                  }];
//
//            [alert addAction:defaultAction];
//            [self presentViewController:alert animated:YES completion:nil];
//        }
//    }];
}

- (void)setupGenderAlertVC{
    
    self.genderAlertVC = [BaseAlertController alertControllerWithTitle:@"" message:@"选择性别" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction * camerAction = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self.header.genderLabel.text = @"男";
        self.header.genderImageView.image = [UIImage imageNamed:@"展开"];
        self.sex = @"1";
        
        if (self.age || self.sex || self.searchStr) {
            
            ShowHUD;
            NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:3];
            
            if (self.age) {
                
                [dict setValue:self.age forKey:@"age"];
            }
            
            if (self.sex) {
                
                [dict setValue:self.sex forKey:@"sex"];
            }
            
            if (self.searchStr) {
                
                [dict setValue:self.searchStr forKey:@"user_name"];
            }
            
            [self searchPersonWithDict:dict.copy];
        }
    }];
    
    UIAlertAction * albumsAction = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self.header.genderLabel.text = @"女";
        self.header.genderImageView.image = [UIImage imageNamed:@"展开"];
        self.sex = @"2";
        if (self.age || self.sex || self.searchStr) {
            
            ShowHUD;
            NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:3];
            
            if (self.age) {
                
                [dict setValue:self.age forKey:@"age"];
            }
            
            if (self.sex) {
                
                [dict setValue:self.sex forKey:@"sex"];
            }
            
            if (self.searchStr) {
                
                [dict setValue:self.searchStr forKey:@"user_name"];
            }
            
            [self searchPersonWithDict:dict.copy];
        }
    }];
    
    UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"不限" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        self.header.genderLabel.text = @"不限";
        self.header.genderImageView.image = [UIImage imageNamed:@"展开"];
//        self.sex = @"";
//        if (self.age || self.sex || self.searchStr) {
//
//            ShowHUD;
//            NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:3];
//
//            if (self.age) {
//
//                [dict setValue:self.age forKey:@"age"];
//            }
//
//            if (self.sex) {
//
//                [dict setValue:self.sex forKey:@"sex"];
//            }
//
//            if (self.searchStr) {
//
//                [dict setValue:self.searchStr forKey:@"user_name"];
//            }
//
//            [self searchPersonWithDict:dict.copy];
//        }
    }];
    
    [self.genderAlertVC addAction:camerAction];
    [self.genderAlertVC addAction:albumsAction];
    [self.genderAlertVC addAction:cancleAction];
}


- (void)setupPivkerView{
    
    self.bgView = [[UIView alloc]init];
    self.bgView.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH / 3);
    self.bgView.backgroundColor = ZRWhiteColor;
    [self.view addSubview:self.bgView];
    
    UIView *btnView = [[UIView alloc]init];
    btnView.frame = CGRectMake(0, 0, kScreenW, 40 * SCREENH6);
    btnView.backgroundColor = ColorWithRGB(0xababab);
    [self.bgView addSubview:btnView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(10 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [sureBtn setTitle:@"取消" forState:UIControlStateNormal];
    [sureBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(self.view.frame.size.width - 70 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:cancelBtn];
    
    float height = kScreenH / 3 - 40 * SCREENH6;
    self.pickerView = [[UIPickerView alloc]init];
    self.pickerView.frame = CGRectMake(0, 40 * SCREENH6, kScreenW, height);
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.bgView addSubview:self.pickerView];
}


// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return self.dataArray.count;
}


// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSDictionary * dict = self.dataArray[row];

//    NSLog(@"dict===%@",dict);
    NSString * bankInfo = [NSString stringWithFormat:@"%@",dict[@"age"]];
    
    return bankInfo;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    _ageIndex = row;
}


- (void)cancelBtn{
    
    self.header.ageImageView.image = [UIImage imageNamed:@"展开"];
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, kScreenW, kScreenH / 3);
    } completion:^(BOOL finished) {
    }];
}


- (void)sureBtn{
    
    int index = (int)_ageIndex;
    if (index != self.dataArray.count - 1) {
        
        NSDictionary * dict = self.dataArray[_ageIndex];
        
        NSString * age = dict[@"age"];
        self.age = dict[@"id"];
        
        self.header.ageLabel.text = age;
        self.header.ageImageView.image = [UIImage imageNamed:@"展开"];
        
        if (self.age || self.sex || self.searchStr) {
            
            ShowHUD;
            NSMutableDictionary * mdict = [NSMutableDictionary dictionaryWithCapacity:3];
            
            if (self.age) {
                
                [mdict setValue:self.age forKey:@"age"];
            }
            
            if (self.sex) {
                
                [mdict setValue:self.sex forKey:@"sex"];
            }
            
            if (self.searchStr) {
                
                [mdict setValue:self.searchStr forKey:@"user_name"];
            }
            
            [self searchPersonWithDict:mdict.copy];
        }
    }else{
        
        self.header.ageLabel.text = @"不限";
        self.header.ageImageView.image = [UIImage imageNamed:@"展开"];
        
        if (DEF_GET_OBJECT(@"lat") && DEF_GET_OBJECT(@"lon")) {
            
            self.showLabel.hidden = YES;
            //定位功能可用
            NSString * lat = DEF_GET_OBJECT(@"lat");
            NSString * lon = DEF_GET_OBJECT(@"lon");
            
            NSDictionary * dict = @{@"address_longitude":lon,@"address_latitude":lat};
            
            [self searchPersonWithDict:dict];
            
        }
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, kScreenH / 3);
    } completion:^(BOOL finished) {
        
    }];
    
}


//- (void)clickAddSearchButton{
//
//    HiddenBar;
//
//    SearchContactsViewController * searchVC = [SearchContactsViewController new];
//
//    searchVC.string = @"搜索昵称";
//
//    [self.navigationController pushViewController:searchVC animated:YES];
//}

- (void)clickSearchButton{
    
    if ([self.status isEqualToString:@"person"]) {
        
        if (self.age || self.sex || self.searchStr) {
            
            ShowHUD;
            NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity:3];
            
            if (self.age) {
                
                [dict setValue:self.age forKey:@"age"];
            }
            
            if (self.sex) {
                
                [dict setValue:self.sex forKey:@"sex"];
            }
            
            if (self.searchStr) {
                
                [dict setValue:self.searchStr forKey:@"user_name"];
            }
            
            [[ZRNetworkTool sharedTool] selectUserWithDict:dict.copy WithBlock:^(BOOL success, id responseData) {

                HiddenHUD;
                if (success) {
                    
                    self.searchArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
                    
                   
                    HiddenBar;
                    SearchPersonViewController * searchVC = [SearchPersonViewController new];
                    
                    searchVC.modelArray = self.searchArray;
                    
                    [self.navigationController pushViewController:searchVC animated:YES];
                    
                }else{
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:responseData[@"message"]                                                                    preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              
                                                                              
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                }
                
            }];
            
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"亲，至少选择一个查找条件"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
    
    
    
}

- (void)changeAddTypeWithType:(NSString *)type{
    
    self.status = type;
}

- (void)pushSearchViewController{
    
    HiddenBar;
    
    SearchContactsViewController * searchVC = [SearchContactsViewController new];
    
    searchVC.string = @"搜索昵称";
    
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)updateData{
    
  if ([self.status isEqualToString:@"person"]) {
      
      if (DEF_GET_OBJECT(@"lat") && DEF_GET_OBJECT(@"lon")) {
          
          self.personArray = nil;
          //定位功能可用
          NSString * lat = DEF_GET_OBJECT(@"lat");
          NSString * lon = DEF_GET_OBJECT(@"lon");
          
          NSDictionary * dict = @{@"address_longitude":lon,@"address_latitude":lat};
          
          [[ZRNetworkTool sharedTool] selectUserWithDict:dict WithBlock:^(BOOL success, id responseData) {
              
              if (success) {
                  
                  self.personArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
              }
          }];
          
      }
  }
}


- (void)getSearchString:(NSString *)str{
    
    if ([self.status isEqualToString:@"person"]) {
        
        ShowHUD;
        
        NSDictionary * dict = @{@"user_name":str};
        [self searchPersonWithDict:dict];
//        [[ZRNetworkTool sharedTool] selectUserWithDict:dict WithBlock:^(BOOL success, id responseData) {
//
//            HiddenHUD;
//            if (success) {
//
//                self.searchArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
//
//
//                HiddenBar;
//                SearchPersonViewController * searchVC = [SearchPersonViewController new];
//
//                searchVC.modelArray = self.searchArray;
//
//                [self.navigationController pushViewController:searchVC animated:YES];
//
//            }else{
//
//                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
//                                                                               message:responseData[@"message"]                                                                    preferredStyle:UIAlertControllerStyleAlert];
//
//                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
//                                                                      handler:^(UIAlertAction * action) {
//
//
//                                                                      }];
//
//                [alert addAction:defaultAction];
//                [self presentViewController:alert animated:YES completion:nil];
//            }
//
//        }];
    }else{
        
        NSString * user_id = DEF_GET_OBJECT(@"id");
        NSDictionary * dict = @{@"user_id":user_id,@"group_name":str};
        
        [[ZRNetworkTool sharedTool] selectGroupWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                self.searchGroupArray = [NSArray yy_modelArrayWithClass:[GroupModel class] json:responseData[@"data"]];
                
                HiddenBar;
                SearchGroupViewController * searchVC = [SearchGroupViewController new];
                
                searchVC.modelArray = self.searchGroupArray;
                
                [self.navigationController pushViewController:searchVC animated:YES];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
}





- (void)clickCategoryButtonWithType:(NSString *)type{
    
    NSString * user_id = DEF_GET_OBJECT(@"id");
    NSDictionary * dict = @{@"user_id":user_id,@"group_category":type};
    self.dataArray = nil;
    [[ZRNetworkTool sharedTool] selectGroupWithDict:dict WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.searchGroupArray = [NSArray yy_modelArrayWithClass:[GroupModel class] json:responseData[@"data"]];
            
            HiddenBar;
            SearchGroupViewController * searchVC = [SearchGroupViewController new];
            
            searchVC.modelArray = self.searchGroupArray;
            
            [self.navigationController pushViewController:searchVC animated:YES];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}


- (void)requestNearbyPeople{
    
    if (DEF_GET_OBJECT(@"lat") && DEF_GET_OBJECT(@"lon")) {
        
        self.showLabel.hidden = YES;
        //定位功能可用
        NSString * lat = DEF_GET_OBJECT(@"lat");
        NSString * lon = DEF_GET_OBJECT(@"lon");
        
        NSDictionary * dict = @{@"address_longitude":lon,@"address_latitude":lat};
        
        [[ZRNetworkTool sharedTool] selectUserWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                self.personArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
            }
        }];
        
    }else{
        
        //定位不能用
        self.showLabel.hidden = NO;
    }
}


- (void)searchPersonWithDict:(NSDictionary *)dict{
    
    self.personArray = nil;
    [[ZRNetworkTool sharedTool] selectUserWithDict:dict WithBlock:^(BOOL success, id responseData) {
        HiddenHUD;
        
        if (success) {
            
            self.personArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)startLocation {
    
    if ([CLLocationManager locationServicesEnabled]) {
        //        CLog(@"--------开始定位");
        self.locationManager = [[CLLocationManager alloc]init];
        self.locationManager.delegate = self;
        //控制定位精度,越高耗电量越
        self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        // 总是授权
        [self.locationManager requestWhenInUseAuthorization];
        self.locationManager.distanceFilter = 10.0f;
        //        [self.locationManager requestAlwaysAuthorization];
        [self.locationManager startUpdatingLocation];
    }
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if ([error code] == kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }
    if ([error code] == kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
}

//定位代理经纬度回调
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    CLLocation *newLocation = locations[0];
    
    //    self.userLocation = newLocation;
    
    double lat = newLocation.coordinate.latitude;
    double lon = newLocation.coordinate.longitude;
    
    NSString * latStr = [NSString stringWithFormat:@"%f",lat];
    NSString * lonStr = [NSString stringWithFormat:@"%f",lon];
    
    DEF_SET_OBJECT(latStr, @"lat");
    DEF_SET_OBJECT(lonStr, @"lon");
    
    NSDictionary * dict = @{@"address_longitude":lonStr,@"address_latitude":latStr};
    
    [[ZRNetworkTool sharedTool] selectUserWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        self.personArray = nil;
        if (success) {
            
            self.personArray = [NSArray yy_modelArrayWithClass:[SearchPersonModel class] json:responseData[@"data"]];
        }
    }];
}

#pragma mark - TableView 占位图
- (NSString *)xy_noDataViewMessage {
    
    return @"";
}



@end
